/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */

package org.ipo.mm.web.servlets;

import com.docmosis.SystemManager;
import com.docmosis.document.DocumentProcessor;
import com.docmosis.document.ProcessingException;
import com.docmosis.document.converter.ConversionException;
import com.docmosis.document.converter.ConversionFormat;
import com.docmosis.document.converter.ConversionInstruction;
import com.docmosis.document.converter.NoConvertersRunningException;
import com.docmosis.document.converter.pool.ConverterPoolException;
import com.docmosis.template.Document;
import com.docmosis.template.TemplateStoreException;
import com.docmosis.template.population.Cleanable;
import com.docmosis.template.population.DataProvider;
import com.docmosis.template.population.DataProviderBuilder;
import com.docmosis.template.store.DropStoreHelper;
import com.docmosis.template.store.TemplateContext;
import com.docmosis.template.store.TemplateIdentifier;
import com.docmosis.template.store.TemplateStoreFactory;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.IOException;
import java.io.InputStream;
import java.io.OutputStream;
import java.sql.Connection;
import java.sql.DriverManager;
import java.util.ArrayList;
import java.util.HashMap;
import java.util.Iterator;
import java.util.Properties;
import java.util.StringTokenizer;
import java.util.logging.Level;
import java.util.logging.Logger;
import javax.servlet.ServletException;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServlet;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import net.sf.jasperreports.engine.JasperRunManager;
import org.ipo.mm.ejb.entities.Meeting;
import org.ipo.mm.ejb.entities.Representative;
import org.ipo.mm.web.util.ConstantManager;

/**
 *
 * @author predrag
 */
public class GetAddressReport extends HttpServlet {
   
  
    protected void processRequest(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
  ServletOutputStream servletOutputStream = response.getOutputStream();
         ArrayList<Representative> representatives = (ArrayList<Representative>) request.getSession().getAttribute(ConstantManager.REQUEST_ENVELOPES_SELECTED_REPRESENTATIVES);
         InputStream reportStream = getServletContext().getResourceAsStream("/WEB-INF/reports/postmail.jasper");

         
         /********************************/
         
            try{
              if(reportStream==null){
                  response.setContentType("text/html;charset=UTF-8");
                  // TODO output your page here

                  servletOutputStream.println("<html>");
                  servletOutputStream.println("<head>");
                  servletOutputStream.println("<title>Report info</title>");
                  servletOutputStream.println("</head>");
                  servletOutputStream.println("<body>");
                  servletOutputStream.println("<h1>Report is not implemented jet !</h1>");
                  servletOutputStream.println("</body>");
                  servletOutputStream.println("</html>");
                  servletOutputStream.close();
                
              }
              else{
                  if(reportStream.available()>0) {
                    //Logger.getLogger(getClass().getName()).log(Level.INFO, "našo report");
                   }
                   else{
                                   
                      try {
                            // TODO output your page here
                            servletOutputStream.println("<html>");
                            servletOutputStream.println("<head>");
                            servletOutputStream.println("<title>Report info</title>");
                            servletOutputStream.println("</head>");
                            servletOutputStream.println("<body>");
                            servletOutputStream.println("<br/><h1>Report is not implemented jet !</h1>");
                            servletOutputStream.println("</body>");
                            servletOutputStream.println("</html>");
                      } finally {
                          servletOutputStream.close();
                        
                      }
                   }
                }
               Class.forName("com.mysql.jdbc.Driver");
               String connStr =  getServletContext().getInitParameter("connectionString");
               Connection connection = DriverManager.getConnection(connStr);
               Logger.getLogger(getClass().getName()).log(Level.INFO, "prije JasperRunManager ");
               HashMap parameterMap = new HashMap();
               String idsInArray="(";

               Iterator itr = representatives.iterator();
               while(itr.hasNext()){
                    Representative rep = (Representative)itr.next();
                    idsInArray+= rep.getId().toString()+",";
               }
               idsInArray+=")";
               idsInArray = idsInArray.replace(",)", ")");
               parameterMap.put("idsInArray", idsInArray);         


              response.setContentType("application/pdf");
              response.addHeader("Content-Disposition","attachment; filename=mailPrint.pdf");
              JasperRunManager.runReportToPdfStream(reportStream,servletOutputStream, parameterMap, connection);

               servletOutputStream.flush();
               servletOutputStream.close();
               

           } catch(Exception e) {
               reportStream.close();
               Logger.getLogger(getClass().getName()).log(Level.INFO, "Exception{0}", e.toString());
               response.setContentType("text/html;charset=UTF-8");
                       try {
                   // TODO output your page here
                   servletOutputStream.println("<html>");
                   servletOutputStream.println("<head>");
                   servletOutputStream.println("<title>Report info</title>");
                   servletOutputStream.println("</head>");
                   servletOutputStream.println("<body>");
                   servletOutputStream.println("<h1>"+e.toString()+"</h1>");
                   servletOutputStream.println("</body>");
                   servletOutputStream.println("</html>");
                  } finally {
                      servletOutputStream.close();
                    
                  }

           }
            finally
            {
                  reportStream.close();
            }
        }  
   
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

   
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

   
    @Override
    public String getServletInfo() {
        return "Short description";
    }
}